package com.titaniume.java;

import org.junit.Test;

import java.io.*;

/**
 * @program: JavaSenior
 * @description:
 *
 *
 * 测试FileInputStream 和FileOutputStream的使用
 *
 *
 *
 * 结论：
 * 1.对于文本文件(.txt,.java,.c,.c++)，使用字符流处理
 * 2.对于非文本文件(.mp4,.jpg,mp3,.avi,.doc,.ppt...)，使用字节流处理
 *
 *
 *
 *
 *
 *
 *
 * @author: renfei.huang
 * @create: 2021-10-17 15:43
 */
public class FileInputStreamTest {



    //使用字节流 FileInputStream处理文本文件 可能出现乱码
    @Test
    public void testFileInputStream()  {
        FileInputStream fileInputStream = null;

        try {
            //1.造文件
            File file = new File("hello.txt");
            //2.造流
            fileInputStream = new FileInputStream(file);

            //3.读取数据
            byte [] buffer = new byte[5];
            int len; //记录每次读取字节的个数
            while ((len = fileInputStream.read(buffer))!=-1){
                String str = new String(buffer, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileInputStream!= null){
                //4.关闭资源
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    /*
    * 实现对图片的复制操作
    * */
    @Test
    public  void testFileInputOutputStream() {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File srcFile = new File("彼岸图网.jpg");
            File destFile = new File("彼岸图网2.jgp");

            //创建输入流和输出流的对象
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);


            //复制过程 读取写入操作
            byte[] buffer = new byte[5];

            int let;
            while ((let = fileInputStream.read(buffer)) != -1)
                fileOutputStream.write(buffer, 0, let);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //指定路径下文件复制
    public  void copyFile(String srcPath,String  destPath){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);

            //创建输入流和输出流的对象
            fileInputStream = new FileInputStream(srcPath);
            fileOutputStream = new FileOutputStream(destFile);


            //复制过程 读取写入操作
            byte[] buffer = new byte[1024];

            int let;
            while ((let = fileInputStream.read(buffer)) != -1)
                fileOutputStream.write(buffer, 0, let);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Test
    public  void testcopyFile(){


        long start = System.currentTimeMillis();

        String srcPath ="D:\\test\\001.mp4";
        String destPath = "D:\\test\\002.mp4";


//        String srcPath ="D:\\test\\hello.txt";
//        String destPath = "D:\\test\\hello2.txt";

        copyFile(srcPath,destPath);

        long end = System.currentTimeMillis();

        System.out.println("复制花费时间为："+ (end - start)); //复制花费时间为：291


    }





}
